<%@ page language="java" %>
<%
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("max-age", 0);
    response.setDateHeader("Expires", 0);
%>

<%@ page import="java.util.ArrayList,
                java.util.HashMap,java.util.Enumeration,
                com.scalar.chiptrack.operations.packingslip.FinalInventoryData,
                com.scalar.chiptrack.utils.ConfigManager,
                com.scalar.chiptrack.utils.StringUtils,
                com.scalar.chiptrack.commons.Constants,
                com.scalar.chiptrack.commons.SessionKeys,
                com.scalar.chiptrack.users.User,
                com.scalar.chiptrack.users.Functionality"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/finalInventory.do"/>

<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>

<%

    ArrayList finalInventoryDataList = (ArrayList) session.getAttribute ("finalInventoryDataList");
	String quantityToBeShipped = (String)request.getParameter("quantityToBeShipped");
    String errMsg= " ";
    String color[] = {"DEE1E2", "FFFFEE"};
	String lineItemId = request.getParameter("lineItemId");



%>
<html:html locale="true">

<HEAD>
	<TITLE>Scalarsoft - ChipTrac Drop Shipment Window</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></SCRIPT>

<script language="JavaScript">


function openAudit (lotNumber,lotId)
{
    var finalInventoryURL = '<%=chipTracContext%>/jsp/operations/lot_qty_pull_audit.jsp?lotId='+lotId+'&lotNumber=' + lotNumber;
    // -- END --
    newWindow = window.open (finalInventoryURL,'lotAudit','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=yes,width=700,height=300,resizable=yes');
    newWindow.focus();
}


function saveValues()
{
	document.finalInventoryForm.txtErrMsg.value='';
	document.finalInventoryForm.totalQtyToBeShipped.disabled = false;
	var totalQtyToBeShpd = parseInt(document.finalInventoryForm.totalQtyToBeShipped.value);
	var lotIdCount = document.finalInventoryForm.lotId.length;
	var lotId = document.finalInventoryForm.lotId;
	var quantity = document.finalInventoryForm.quantity;
	var locationId = document.finalInventoryForm.locationId;
	var quantityNeeded = 0;
    var sum = 0;
    var flag=0;
	// var lineItemCount = fldName1Val.substring(fldName1Val.indexOf('_')+1);
	if ( !checkLotNumbers() )
		return;
	if (document.finalInventoryForm.quantity.length == null) 
    {
        if( checkInteger(document.finalInventoryForm.quantity.value,"Quantity Pull","N")==false)
        {	
			document.finalInventoryForm.txtErrMsg.value='Enter only integer values';
			document.finalInventoryForm.quantity.focus();
        }
        else
        {
	        sum = document.finalInventoryForm.quantity.value;

			var fldName1Val = document.finalInventoryForm.fieldName1.value;
			var lineItemCount = fldName1Val.substring(fldName1Val.indexOf('_')+1);


			if (parseInt(sum) != parseInt(totalQtyToBeShpd))
			{
				document.finalInventoryForm.txtErrMsg.value='Quantity needed should be equal to total quantity to be shipped';
				document.finalInventoryForm.quantity.focus();
			}
            else
            {
				var qtyToBePulled = eval('window.opener.document.dropShipmentPSForm.qtyPulled_'+lineItemCount);
				qtyToBePulled.value = sum;
				var fieldValue = eval('window.opener.document.dropShipmentPSForm.' + document.finalInventoryForm.fieldName.value);
				fieldValue.value = sum;
				var field1Value = eval('window.opener.document.dropShipmentPSForm.' + document.finalInventoryForm.fieldName1.value);
				field1Value.value = sum;
                document.finalInventoryForm.submit();
                window.close();
            }
        }
    }
	
    else 
    {
        for( var i=0; i < document.finalInventoryForm.quantity.length; i++ ) 
        {
            if (document.finalInventoryForm.quantity[i].value.trim().length != 0) 
            {               
                if(checkInteger(document.finalInventoryForm.quantity[i].value,"Quantity Pull","N")==false)
                {	
					document.finalInventoryForm.txtErrMsg.value='Enter only integer values';
					document.finalInventoryForm.quantity[i].focus();
					flag=1;
					break;
                }
                else
                {   
                    if (document.finalInventoryForm.quantity[i].value.trim().length != 0) 
                    {
                        sum += parseInt (document.finalInventoryForm.quantity[i].value);
                    }
                }
            }
        }
        
        if(flag==0)
        {
            if( parseInt( sum ) != parseInt( totalQtyToBeShpd ) )
            {
                document.finalInventoryForm.txtErrMsg.value='Total should be equal to Quantity to be shipped';
            }
            else
            {
				var fldName1Val = document.finalInventoryForm.fieldName1.value;
				var lineItemCount = fldName1Val.substring(fldName1Val.indexOf('_')+1);
				var qtyToBePulled = eval('window.opener.document.dropShipmentPSForm.qtyPulled_'+lineItemCount);
				qtyToBePulled.value = sum;
				var fieldValue = eval('window.opener.document.dropShipmentPSForm.' + document.finalInventoryForm.fieldName.value);
				fieldValue.value = sum;

				var field1Value = eval('window.opener.document.dropShipmentPSForm.' + document.finalInventoryForm.fieldName1.value);
				field1Value.value = sum;
                document.finalInventoryForm.submit();
                window.close();
            }
        } 
    }  
}



function addTextArea ( locationCount, locationId, locationDetail ) 
{
	var htmltext = '<div><table border="0" width="100%"><tr>';
	htmltext = htmltext + '<td colspan="3" align="center"><input type="text" name="lotNumber_'+locationCount+'" class="txfield">';
	htmltext = htmltext + '<input type="hidden" name="locationId" value='+locationId+'>';
	htmltext = htmltext + '<input type="hidden" name="locationDetails" value="'+locationDetail+'">';
	htmltext = htmltext + '<input type="hidden" name="lotId" value="-1"></td>';
	htmltext = htmltext + '<td colspan="3" align="center"><input type="text" name="quantity" class="valuetxfield">'; 
	htmltext = htmltext + '</td></tr></table></div>';
	var pa=document.getElementById("font_"+locationCount);
	pa.insertAdjacentHTML('BeforeEnd',htmltext);
}


function checkLotNumbers()
{
	var locCount = parseInt(document.finalInventoryForm.locationsCount.value);
	var quantityCount = 0;
	for ( var loc_Cnt =0; loc_Cnt < locCount; loc_Cnt++ )
	{
		var lot_NameArray = eval ('document.finalInventoryForm.lotNumber_'+loc_Cnt);
		if (lot_NameArray.length == null)
		{
			if ( document.finalInventoryForm.quantity.length == null )
			{
				if ( (document.finalInventoryForm.quantity.value.trim().length > 0 ) && ( parseInt (document.finalInventoryForm.quantity.value) != 0 ) )
				{
					if ( lot_NameArray.value.trim().length == 0 )
					{
						document.finalInventoryForm.txtErrMsg.value='Please Enter a Lot Number and Zero quantity pull is not allowed';
						lot_NameArray.focus();
						return false;
					}
				}				
			}
			else
			{
				if ( (document.finalInventoryForm.quantity[quantityCount].value.trim().length > 0 ) && ( parseInt (document.finalInventoryForm.quantity[quantityCount].value) != 0 ) )
				{
					if ( lot_NameArray.value.trim().length == 0 )
					{
						document.finalInventoryForm.txtErrMsg.value='Please Enter a Lot Number and Zero quantity pull is not allowed';
						lot_NameArray.focus();
						return false;
					}
				}
				quantityCount++;
			}
		}//if ends
		else
		{
			for (var lot_NumCount = 0; lot_NumCount<lot_NameArray.length; lot_NumCount++ )
			{
				var qty = document.finalInventoryForm.quantity[quantityCount].value;
				if ( (qty.trim().length > 0 ) && ( parseInt (qty) != 0 ) )
				{
					if ( lot_NameArray[lot_NumCount].value.trim().length == 0 )
					{
						document.finalInventoryForm.txtErrMsg.value='Please Enter a Lot Number';
						lot_NameArray[lot_NumCount].focus();
						return false;
					}
				}
				for( var lot_NumCount1=0; lot_NumCount1 < lot_NameArray.length; lot_NumCount1++ ) 
				{
					if ( parseInt(lot_NumCount1) != parseInt(lot_NumCount) )
					{
						if (lot_NameArray[lot_NumCount].value.trim() == lot_NameArray[lot_NumCount1].value.trim())
						{
							document.finalInventoryForm.txtErrMsg.value='Do not enter the Same Lot Number for the Same Location';
							lot_NameArray[lot_NumCount1].focus();
							return false;
						}
					}
				}//inner for loop ends
				quantityCount++;
			}//outer for loop ends
		}//end of else
	}//main for loop ends
	return true;
}



    </script>

</HEAD>
<BODY bgcolor="ffffff" marginheight="0" marginwidth="0" topmargin=0 leftmargin=0>

<table align="center" border="0" width="60%" bordercolor="#FFFFFF">

<html:form name="finalInventoryForm" type="com.scalar.chiptrack.ChipTrackActionForm" method="post" action="/finalInventory.do?action=dsSave">
<% 
User user1 = (User)session.getAttribute (SessionKeys.USER_KEY);
if ( user1.isActionAllowed ( Functionality.PACKING_SLIP, User.USER_ACTION_MODIFY  ) == false ) { %> 
<tr><td colspan='<%out.print("6");%>'> 
<font class='NArialS'><font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b>
</font></td></tr>  
<% } %>
    <tr><td colspan='<%out.print("6");%>'><chiptrack:errors styleClass="NArialL"/></td></tr>
    <tr><td colspan='<%out.print("6");%>'><INPUT align=left border=0 
                        maxLength=100 name="txtErrMsg" size=100 
                        style="BORDER-BOTTOM: thin; BORDER-LEFT-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-TOP: thin; CLEAR: none; COLOR: red;font-family: arial;font-size: 10pt;font-weight:bold " value="" readOnly>
    </td></tr>
	<% int locationCount = Integer.parseInt(request.getAttribute("locationCount").toString());%>

    <tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=5></td></tr>
    <tr><td colspan='<%out.print("6");%>' align='center'><font class="NArialL">Drop Shipment Screen</font></td></tr>
    <tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=5></td></tr>

		<!--boomtr>
		<td colspan="5" align='right'><font class='NArialL'>Total Quantity to be Shipped:</font></td>
		<td colspan="2"><input type="text" size='4' name="totalQtyToBeShipped" class='NArialL' style="BORDER-BOTTOM: thin; BORDER-LEFT-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-TOP: thin; CLEAR: none; COLOR: red;font-family: arial;font-size: 10pt;font-weight:bold " value='<%=request.getParameter("prevQtyShipped")%>' readOnly disabled></td></trboom-->
	 
	 <tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
	 
	 <tr><td colspan='<%out.print("6");%>' align="center" bgcolor="#993333"><font class="NArialL">
	 <font color="#FFFFFF"><%=request.getParameter("mktPartNumber")%></font>

		<input type="hidden" name="mktPartNumber" value="<%=request.getParameter("mktPartNumber")%>">
		<input type="hidden" name="lineItemId" value="<%=lineItemId%>">
		<input type="hidden" name="actualCsLineItemId" value='<%=request.getParameter ("csLineItemId")%>'/>
		<input type="hidden" name="fieldName" value='<%=request.getParameter ("fieldName")%>'/>
		<input type="hidden" name="fieldName1" value='<%=request.getParameter ("fieldName1")%>'/>
		<input type="hidden" name="locationsCount" value='<%=locationCount%>'/>
		&nbsp;</td></tr>
		<tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
		<% for (int totalLocations=0; totalLocations < locationCount; totalLocations++ ) { %>
		<%String location_and_id = request.getAttribute("loc_"+totalLocations).toString();
		String locationId = location_and_id.substring(0, location_and_id.indexOf("$")); 
		String location = location_and_id.substring(location_and_id.indexOf("$")+1); 
		ArrayList lotDataList = (ArrayList)request.getAttribute("lot_"+totalLocations);
		int LotCount = 0;//to pass the value that will decide the colour of the row.
		%>
		<tr><td colspan='<%out.print("6");%>' bgcolor="#999CCC" align='left'><font class='NArialL'>
		<font color='#000000'><%=location%></font>
		&nbsp;</td></tr>
		<tr>
	    <td align="center" colspan='<%out.print("3");%>' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Lot Number</font></td>
		<td align="center" colspan='<%out.print("3");%>' bgcolor="#993333"><font class="NArialL"><font color="#FFFFFF">Quantity needed</font></td>
		</tr>

		
		<% 
		for (int lotCount =0; lotCount < lotDataList.size(); lotCount++)
		{
			FinalInventoryData finalInventoryData = (FinalInventoryData)lotDataList.get(lotCount); %>
			<tr>
			<td colspan='<%out.print("3");%>' align="center" bgcolor='<%=color[lotCount%2]%>'>
			<input type="hidden" name="locationId" value="<%=locationId%>">
			<input type="hidden" name="locationDetails" value="<%=location_and_id%>">
			<% if (finalInventoryData.getLotNumber()!=null && (finalInventoryData.getLotNumber().trim().length() > 0) ) { %>
				<input type="hidden" name="lotId" value="<%=finalInventoryData.getLotId()%>">
				<input type="hidden" name="lotNumber_<%=totalLocations%>" value="<%=finalInventoryData.getLotNumber()%>">
				<a href=JavaScript:openAudit('<%=finalInventoryData.getLotNumber()%>','<%=finalInventoryData.getLotId()%>') title='Click here for <%=finalInventoryData.getLotNumber()%> Lot Audit Information'>
				<font class="NArialL" bgcolor='<%=color[lotCount%2]%>'><%=finalInventoryData.getLotNumber()%></font></a> &nbsp;</td>
				<td colspan='<%out.print("3");%>' align="center" bgcolor='<%=color[lotCount%2]%>'>
				<input type="text" name="quantity" value='<%= ( finalInventoryData.getQuantityPull() >= 1 ) ? 
				"" + finalInventoryData.getQuantityPull() : ""%>' class="valuetxfield" size="20">
				<!--This hidden parameter is being added to do a combined validation for the lot Number and quantity
				put together. Means it will not mess up with the existing "quantity" text field defined above.-->
				<!--input type="hidden" name="quantity_< %=totalLocations%>" value='< %= ( finalInventoryData.getQuantityPull() >= 1 ) ? 
				"" + finalInventoryData.getQuantityPull() : ""%>'-->
				</td></tr>
			<%} else { %><!--end of inner if condition-->
				<input type="hidden" name="lotId" value="-1">
				<input type="text" name="lotNumber_<%=totalLocations%>" value="" class="txfield">
				</td>
				<td colspan='<%out.print("3");%>' align="center" bgcolor='<%=color[lotCount%2]%>'>
				<input type="text" name="quantity" value='<%= ( finalInventoryData.getQuantityPull() >= 1 ) ? 
				"" + finalInventoryData.getQuantityPull() : ""%>' class='valuetxfield' size="20">
				<!--This hidden parameter is being added to do a combined validation for the lot Number and quantity
				put together. Means it will not mess up with the existing "quantity" text field defined above.-->
				<!--input type="hidden" name="quantity_< %=totalLocations%>" value='< %= ( finalInventoryData.getQuantityPull() >= 1 ) ? 
				"" + finalInventoryData.getQuantityPull() : ""%>'-->
				</td></tr>
			<% } %>
		<% } %><!--end of inner for loop-->
        <tr><td colspan='6' align="left"><FONT id=<%="font_"+totalLocations%>></FONT></TD></TR>
		<tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=20></td></tr>
 
           
        <tr><td colspan='<%out.print("6");%>' align="right">
		<img src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" border="0" onclick='javascript:addTextArea(<%=totalLocations%>,
		<%=locationId%>, "<%=location_and_id%>")'>
		</td></tr>

    <%
        }//outer for loop ends
    %>
    
	<tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10></td></tr>
	<%if (locationCount != 0 ) {%>
		<tr>
		<td colspan="5" align='right'><font class='NArialL'>Total Quantity to be Shipped:</font></td>
		<td colspan="2"><input type="text" size='4' name="totalQtyToBeShipped" class='NArialL' style="BORDER-BOTTOM: thin; BORDER-LEFT-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-TOP: thin; CLEAR: none; COLOR: red;font-family: arial;font-size: 10pt;font-weight:bold " value='<%=request.getParameter("prevQtyShipped")%>' readOnly disabled></td></tr>

		<tr><td colspan='<%out.print("6");%>' align="center">
		  <chiptrack:restrictedLink functionality='<%=Functionality.PACKING_SLIP%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:saveValues();" onmouseover="window.status='Click here to save';return true" onmouseout ="window.status='';return true"> <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17"></chiptrack:restrictedLink>
          <a href="javascript:reset();" title='Click here to reset all field values'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17"></a>
          <a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"></a>
		</td></tr>
	<% } else { %>
		<tr><td colspan='<%out.print("6");%>' align='center'><font class="NArialL" color="red">No Locations found Contact System Administrator.</font></td></tr>
		<tr><td colspan='<%out.print("6");%>' align="center">
          <a href="javascript:close()" title='Click here to close the window'><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"></a>
		</td></tr>
	<% } %>

    <tr><td colspan='<%out.print("6");%>'><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=30></td></tr>
</html:form>
</table>
</body>
</html:html>